home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_03 / stout / olymfilt.hpp < prev   
C/C++ Source or Header  |  1995-02-06  |  3KB  |  85 lines

  1. //
  2. //  OLYMFILT.HPP - Header file for 16-bit "Olympic" filter function.
  3. //
  4.  
  5. #include <limits.h>
  6. #include "circbuf.hpp"
  7.  
  8. typedef enum {OBUF_TINY = 4, OBUF_SMALL = 6, OBUF_MEDIUM = 10,
  9.               OBUF_LARGE = 18, OBUF_HUGE = 34} OSIZE;
  10.  
  11. #ifndef __BORLANDC__
  12.  template<class T> max(T a, T b) {return (a > b) ? a : b; };
  13.  template<class T> min(T a, T b) {return (a < b) ? a : b; };
  14. #endif
  15.  
  16. class OlympicFilt_T : public cbuf_t<short>
  17. {
  18. public:
  19.       OlympicFilt_T(OSIZE);
  20.       short OlympicFilt();
  21. };
  22.  
  23. /************************************************************************/
  24. /*                                                                      */
  25. /*  OlympicBuf() Circular buffer constructor.                           */
  26. /*                                                                      */
  27. /*  Arguments: 1 - Number of elements in the circular buffer.           */
  28. /*                                                                      */
  29. /************************************************************************/
  30.  
  31. OlympicFilt_T::OlympicFilt_T(OSIZE len = OBUF_HUGE)
  32. {
  33.       init((size_t)len);
  34. }
  35.  
  36. /************************************************************************/
  37. /*                                                                      */
  38. /*  OlympicFilt() - Function to perform an "Olympic" filter on the      */
  39. /*                  data in a circular buffer.                          */
  40. /*                                                                      */
  41. /************************************************************************/
  42.  
  43. short OlympicFilt_T::OlympicFilt()
  44. {
  45.       size_t i;
  46.       long accum;
  47.       short *obuf  = new short[size * sizeof(short)];
  48.       short bufmin = SHRT_MAX, bufmax  = SHRT_MIN;
  49.  
  50.       // cbuf_t::buf may be subject to asynchronous modification,
  51.       //  so take a snapshot of it.
  52.  
  53.       memcpy(obuf, buf, size * sizeof(short));
  54.       for (i = 0, accum = 0L; i < size; ++i)
  55.       {
  56.             accum += obuf[i];
  57.             bufmin = min(obuf[i], bufmin);
  58.             bufmax = max(obuf[i], bufmax);
  59.       }
  60.       accum -= bufmin;
  61.       accum -= bufmax;
  62.       switch (size)
  63.       {
  64.       case OBUF_TINY:
  65.             return (short)(accum >> 1);
  66.  
  67.       case OBUF_SMALL:
  68.             return (short)(accum >> 2);
  69.  
  70.       case OBUF_MEDIUM:
  71.             return (short)(accum >> 3);
  72.  
  73.       case OBUF_LARGE:
  74.             return (short)(accum >> 4);
  75.  
  76.       case OBUF_HUGE:
  77.             return (short)(accum >> 5);
  78.  
  79.       default:
  80.             return(short)(accum / (size - 2));
  81.       }
  82. }
  83.  
  84.  
  85.